﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;

namespace Libs.HR
{
    public class ProfileLog
    {
        public int LogID { get; set; }
        public int ProfileID { get; set; }
        public int EmployeeID { get; set; }
        public DateTime DeliveryTime { get; set; }
        public DateTime LogTime { get; set; }
        public bool Status { get; set; }
        public int FileID { get; set; }
        public int ReturnValue { get; set; }

        public ProfileLog()
        {

        }

        ~ProfileLog()
        {

        }

        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Lấy giá trị ProfileLog theo ID
        /// </summary>
        /// <param name="LogID"></param>
        /// <returns></returns>
        public ProfileLog Get(int logID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<ProfileLog>("sp_ProfileLog_Select",
                new SqlParameter("@LogID", logID));
        }

        /// <summary>
        /// Lấy giá trị ProfileLog theo ID
        /// </summary>
        /// <returns></returns>
        public ProfileLog Get()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetInstanceSP<ProfileLog>("sp_ProfileLog_Select",
                new SqlParameter("@LogID", LogID));
        }

        /// <summary>
        /// Thêm mới ProfileLog
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[6];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@ProfileID", ProfileID);
            pars[2] = new SqlParameter("@EmployeeID", EmployeeID);
            pars[3] = DeliveryTime == DateTime.MinValue ? new SqlParameter("@DeliveryTime", DBNull.Value) : new SqlParameter("@DeliveryTime", DeliveryTime);
            pars[4] = new SqlParameter("@Status", Status);
            pars[5] = FileID == 0 ? new SqlParameter("@FileID", DBNull.Value) : new SqlParameter("@FileID", FileID);

            db.ExecuteNonQuerySP("sp_ProfileLog_Insert", pars);
            LogID = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Cập nhật ProfileLog
        /// </summary>
        public void Update()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            SqlParameter[] pars = new SqlParameter[7];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@LogID", LogID);
            pars[2] = new SqlParameter("@ProfileID", ProfileID);
            pars[3] = new SqlParameter("@EmployeeID", EmployeeID);
            pars[4] = DeliveryTime == DateTime.MinValue ? new SqlParameter("@DeliveryTime", DBNull.Value) : new SqlParameter("@DeliveryTime", DeliveryTime);
            pars[5] = new SqlParameter("@Status", Status);
            pars[6] = new SqlParameter("@FileID", FileID);

            db.ExecuteNonQuerySP("sp_ProfileLog_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Xoá ProfileLog
        /// </summary>
        /// <param name="logID"></param>
        public void Delete(int logID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_ProfileLog_Delete",
                new SqlParameter("@LogID", logID));
        }

        /// <summary>
        /// Xoá ProfileLog
        /// </summary>
        public void Delete()
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);

            db.ExecuteNonQuerySP("sp_ProfileLog_Delete",
                new SqlParameter("@LogID", LogID));
        }

        public DataTable GetTList(int employeeID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetDataTableSP("sp_ProfileLog_SelectList",
                new SqlParameter("@EmployeeID", employeeID));
        }

        public DataTable GetTListRemaining(int employeeID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetDataTableSP("sp_ProfileLog_SelectListRemaining",
                new SqlParameter("@EmployeeID", employeeID));
        }

        public List<ProfileLog> GetList(int employeeID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetListSP<ProfileLog>("sp_ProfileLog_SelectList",
                new SqlParameter("@EmployeeID", employeeID));
        }

        public DataTable GetTListReport(int departmentID)
        {
            DbHelper db = new DbHelper(Config.HRConnectionStrings);
            return db.GetDataTableSP("sp_ProfileLog_Report",
                departmentID == 0 ? new SqlParameter("@DepartmentID", DBNull.Value) : new SqlParameter("@DepartmentID", departmentID));
        }

    }
}
